#!/usr/bin/env python2.5
# -*- coding: UTF-8 -*-  
#from google.appengine.api import users
from  milonpy import NMdb
reload(NMdb)
#from google.appengine.ext import deferred 
db=NMdb.db   
class AuthToken(db.Model):
    """Auth Token. 
    A temporary auth token that we will use to authenticate a user with a
    third party website. (We need to store the data while the user visits
    the third party website to authenticate themselves.) 
    TODO: Implement a cron to clean out old tokens periodically.
    """ 
    service   = db.StringProperty(required=True)
    token     = db.StringProperty(required=True)
    secret    = db.StringProperty(required=True)
    created   = db.DateTimeProperty(auto_now_add=True)


                      
class ApplService(NMdb.nmModel):
    application_code         = db.StringProperty(required=True, indexed=False,verbose_name='Unique Application Code Name')
    application_title        = db.StringProperty(required=False, indexed=False,verbose_name='Application Name')
    application_description  = db.TextProperty(required=False)
    service_provider         = db.StringProperty(required=True, indexed=False)  
    consumer_key             = db.StringProperty(required=False, indexed=False)
    consumer_secret          = db.StringProperty(required=False, indexed=False)
    url_request              = db.LinkProperty(required = False,indexed=False)
    url_access               = db.LinkProperty(required = False,indexed=False)        
    url_authorize            = db.LinkProperty(required = False, indexed=False) 
        
    def put(self, **kwargs ):
        self._key_name=self.service_provider+"|"+self.application_code
        return super(ApplService, self).put(**kwargs) 
        
class ApplUser  (NMdb.nmModel):
    """TODO set key to appkey + userID then query by key"""
    appkey=     db.ReferenceProperty   (ApplService, required=True)  
    apSpcD=     NMdb.DicPropertyEval   (indexed=False, required=False)
    user=       db.UserProperty        (indexed=False, auto_current_user_add=True) 
    userID=     db.StringProperty      (required=False,indexed=True, verbose_name='user ID Coz Email can change')
    tokn=       db.StringProperty      (required=True, indexed=False,verbose_name='user token')
    secr=       db.StringProperty      (required=True, indexed=False,verbose_name='user secret') 
    accTP=      db.StringProperty      (required=False,indexed=False,verbose_name='acount Type i.e. twitter, gmail etc')

class msgSMS    (NMdb.nmModel):
    user=       db.ReferenceProperty   (ApplUser, required=False)  
    #msgToUsr=   db.ReferenceProperty   (ApplUser, required=False)   # can have 2 refs  but ....
    msgToNum=   db.PhoneNumberProperty (required=True, indexed=True)
    msgTxt=     db.StringProperty      (required=True, indexed=False)
    msgSid=     db.StringProperty      (required=False,verbose_name='Provider SessionID')
    msgSes=     NMdb.DicPropertyEval   (indexed=False, required=False, verbose_name="Session from Provider")
    dtCr=       db.DateTimeProperty    (auto_now_add=True, verbose_name='Date Time Created')     
    dtSend=     db.DateTimeProperty    (auto_now_add=True, verbose_name='Date Time Send')
    result=     NMdb.DicPropertyEval   (indexed=False, required=False, verbose_name="Result from Provider")
    status=     db.IntegerProperty     (indexed=True, required=True)
    
    
